package com.sctvcloud.weiyuan.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sctvcloud.weiyuan.R;
import com.sctvcloud.weiyuan.ui.activities.LoginActivity;
import com.sctvcloud.weiyuan.utils.UserManager;

/**
 * 文明实践栏目
 *
 * @author suming
 * @date 2020/6/16 14:26
 */
public class CivilizedPracticeFragment extends ColumnBaseFragment {
    private static final String TAG = CivilizedPracticeFragment.class.getSimpleName();
    private static final String BASE_URL = "http://wmsj.scwy.tv:8081/dist/#/home";
    private static final int REQUEST_CODE_LOGIN = 111;
    private WebView mWebView;

    public static CivilizedPracticeFragment newInstance() {
        Bundle args = new Bundle();
        CivilizedPracticeFragment fragment = new CivilizedPracticeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_civilized_practice, container, false);
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        super.initViews(v, savedInstanceState);
        mWebView = v.findViewById(R.id.webView);
        initSettings();
        if (UserManager.getInstance().isLogin() && null != UserManager.getInstance().getUser()) {
            String url = BASE_URL + "?utk=" + UserManager.getInstance().getUser().getToken();
            mWebView.loadUrl(url);
        } else {
            String url = BASE_URL;
            mWebView.loadUrl(url);
        }
    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
        if (UserManager.getInstance().isLogin() && null != UserManager.getInstance().getUser()) {
            String url = BASE_URL + "?utk=" + UserManager.getInstance().getUser().getToken();
            mWebView.loadUrl(url);
        } else {
            String url = BASE_URL;
            mWebView.loadUrl(url);
        }
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initSettings() {
        WebSettings webSetting = mWebView.getSettings();
        String ua = webSetting.getUserAgentString();
        webSetting.setUserAgentString(ua + " SCGC_UA");
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(false);
        webSetting.setBuiltInZoomControls(false);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(false);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setAppCachePath(getContext().getDir("appcache", 0).getPath());
        webSetting.setDatabasePath(getContext().getDir("databases", 0).getPath());
        webSetting.setGeolocationDatabasePath(getContext().getDir("geolocation", 0)
                .getPath());
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    private void loadWeb() {
        if (UserManager.getInstance().isLogin() && null != UserManager.getInstance().getUser()) {
            String url = BASE_URL + "?utk=" + UserManager.getInstance().getUser().getToken();
            mWebView.loadUrl(url);
        } else {
            Intent intent = new Intent(getContext(), LoginActivity.class);
            startActivityForResult(intent, REQUEST_CODE_LOGIN);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_LOGIN) {
            if (resultCode == LoginActivity.RESULT_OK) {
                if (UserManager.getInstance().isLogin() && null != UserManager.getInstance().getUser()) {
                    String url = BASE_URL + "?utk=" + UserManager.getInstance().getUser().getToken();
                    mWebView.loadUrl(url);
                } else {
                    String url = BASE_URL;
                    mWebView.loadUrl(url);
                }
            }
        }
    }

}
